import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth';
import { ElMessage } from 'element-plus';

import Home from '@/views/front/Home/index.vue'
import PetList from '@/views/front/PetList/index.vue'
import AdoptList from '@/views/front/Adopt/index.vue'
import User from '@/views/User/index.vue'
import Login from '@/views/User/Login/index.vue'
import Register from '@/views/User/Register/index.vue'
import Person from '@/views/front/Person/index.vue'
import ApplyManager from '@/views/front/Adopt/applyManage.vue'
import ApplyData from '@/views/front/Adopt/applyData.vue'

import BackHome from '@/views/back/Home/index.vue';
import BackPetIndex from '@/views/back/PetList/index.vue';
import BackPetList from '@/views/back/PetList/PetList.vue';
import BackPetOperate from '@/views/back/PetList/PetOperate.vue';
import BackAdoptIndex from '@/views/back/Adopt/index.vue';
import BackAdoptApplyList from '@/views/back/Adopt/ApplyList.vue';
import BackAdoptApplyData from '@/views/back/Adopt/ApplyData.vue';
import BackUserManager from '@/views/back/User/index.vue';
import BackPerson from '@/views/back/Person/index.vue';

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            component: Home,
            children: [
                {
                    path: '',
                    component: PetList,
                    meta: { 
                        title: '宠物列表', 
                        iconPath: '/icon/paw-sharp-blue.png',
                      }
                },
                {
                    path: 'adopt',
                    component: AdoptList, 
                    meta: { 
                        title: '申请管理', 
                        iconPath: '/icon/Application-blue.png',
                    },
                    children: [
                        {
                            path: '',
                            component: ApplyManager,
                        },
                        {
                          path: 'apply-data', 
                          component: ApplyData,
                          meta: { 
                            title: '申请资料', 
                          },
                        }
                    ]
                },
                {
                    path: 'person',
                    component: Person,
                    meta: { 
                        title: '个人资料', 
                        iconPath: '/icon/user-blue.png',
                      }
                },
            ]
        },
        {
            path: '/back',
            component: BackHome,
            children: [
                {
                    path: '',
                    component: BackPetIndex,
                    children: [
                        {
                            path: '',
                            component: BackPetList,
                            meta: {
                                title: ''
                            }
                        },
                        {
                            path: 'pet-operate',
                            component: BackPetOperate,
                            meta: {
                                title: '宠物管理'
                            }
                        }
                    ],
                    meta: { 
                        title: '宠物管理', 
                        iconPath: '/icon/paw-sharp-blue.png',
                    }
                },
                {
                    path: 'adopt',
                    component: BackAdoptIndex,
                    meta: {
                        title: '领养管理',
                        iconPath: '/icon/Application-blue.png',
                    },
                    children: [
                        {
                            path: '',
                            component: BackAdoptApplyList,
                        },
                        {
                            path: 'apply-data',
                            component: BackAdoptApplyData,
                            meta: {
                                title: '申请资料'
                            }
                        },
                    ]
                },
                {
                    path: 'user',
                    component: BackUserManager,
                    meta: {
                        title: '用户管理',
                        iconPath: '/icon/users-blue.png',
                    }
                },
                {
                    path: 'person',
                    component: BackPerson,
                    meta: {
                        title: '个人资料',
                        iconPath: '/icon/user-blue.png',
                    }
                }
            ]
        },
        {
            path: '/user',
            component: User,
            children: [
                {
                    path: 'login',
                    component: Login,
                },
                {
                    path: 'register',
                    component: Register,
                },
            ]
        },
        
    ]
});

// 路由守卫
router.beforeEach((to, from, next) => {
    const userStore = useAuthStore();
    const isLogin = !!userStore.token;
    const role = userStore.role || 'USER';

    // 未登录：只能访问登录页和注册页
    if(!isLogin && to.path !== '/user/login' && to.path !== '/user/register') {
        next('/user/login');
        return;
    }

    // 已登录：角色校验
    if(role === 'ADMIN') {    // 管理员无限制
        userStore.updateLastPath(to.path);
        next();
        return;
    }

    // 普通用户拦截管理后台路径
    if(role === 'USER' && to.path.startsWith('/back')) {
        ElMessage.error('权限不足，无法访问');
        next(userStore.LastPath || '/'); 
        return;
    }

    userStore.updateLastPath(to.path);
    next();
});

export default router